package com.wan.core.strategy.selector;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * 单策略选择器
 */
public final class StrategySingleSelector<K, T> implements Selector<K, T> {

    private T value;

    public StrategySingleSelector(T value) {
        this.value = value;
    }

    public <Ex extends T> StrategySingleSelector(Collection<T> values, Class<Ex> cls, Supplier<T> supplier) {
        Optional<T> first = values.stream().filter(t -> t.getClass() == cls).findFirst();
        this.value = first.orElse(supplier.get());
    }

    @Override
    public Optional<T> get(K k) {
        return Optional.ofNullable(value);
    }

    @Override
    public List<T> getAll() {
        return Collections.singletonList(value);
    }

    @Override
    public void add(K k, T t) {
        value = t;
    }

    @Override
    public void remove(K k, T t) {
    }


}
